//
//  FLLRecommendViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/1.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLRecommendViewController.h"
#import "ZWYNetTool.h"
#import "FLLHomeModel.h"
#import "AFNetworking.h"
#import "Widget_Data.h"
#import "Widgetlist.h"
#import "Banner.h"
#import "HomeData.h"
#import "Masonry.h"
#import "FLLMacro.h"
#import "FLLButtonAndLabelView.h"
#import "FLLRecommendTableViewCell.h"
#import "FLLFiveItemsTableViewCell.h"
#import "FLLExpertTableViewCell.h"
#import "FLLFoodTableViewCell.h"
#import "FLLSelectionTableViewCell.h"
#import "FLLForthSectionHeaderView.h"
#import "FLLFifthSectionHeaderView.h"
#import "FLLSectionHeaderView.h"
#import "FLLChoseItemCollectionViewCell.h"
#import "FLLHorizontalCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "FLLSectionFooterView.h"
#import "UILabel+FLLSetPropety.h"
#import "FLLCycleImage.h"
#import "FLLGreenHandViewController.h"
#import "FLLSelectedMoreViewController.h"
#import "FLLSceneViewController.h"
#import "FLLMatchViewController.h"
#import "FLLLikeViewController.h"
#import "MJRefresh.h"
#import "FLLDetailViewController.h"
#import "FLLFoodWebVC.h"
#import "FLLFoodMoreViewController.h"
#import "FLLSceneCollectionViewCell.h"
#import "FLLSceneInfoViewController.h"
#import "FLLMakeFoodViewController.h"
#import "FLLMethodViewController.h"
#import "FLLSearchViewController.h"
#import "FLLRecommendTalentViewController.h"
#import "FLLVideoViewController.h"


@interface FLLRecommendViewController ()<UITableViewDataSource, UITableViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UISearchBarDelegate>
@property (nonatomic, strong) UITableView *recommendTableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *cycleView;
@property (nonatomic, strong) UIView *choseItemView;
@property (nonatomic, strong) UICollectionView *choseItemCollectionView;
@property (nonatomic, strong) UIView *horizontalView;
@property (nonatomic, strong) UICollectionView *horizontalCollectionView;
@property (nonatomic, strong) FLLHomeModel *fllHomeModel;
@property (nonatomic, strong) FLLCycleImage *cycleImage;
@property (nonatomic, strong) NSMutableArray *iconItemArray;
@property (nonatomic, strong) NSMutableArray *itemNameArray;
@property (nonatomic, strong) NSMutableArray *horizontalArray;
@property (nonatomic, strong) NSArray<Widget_Data *> *foodArray;
@property (nonatomic, strong) NSMutableArray *todayIDArray;
@property (nonatomic, strong) UISearchBar *searchBar;
@end

@implementation FLLRecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
     // 网络请求
    [self createPOSTRequest];
    [self createRecommendTableView];
    
    // 创建头视图
    [self createHeaderView];

    
#warning mark -- Home 网络请求
    

}
- (void)createPOSTRequest
{
       // 网络请求:
        [ZWYNetTool POST:@"http://api.izhangchu.com/?appVersion=4.5&sysVersion=9.0.2&devModel=iPhone" andBody:@"methodName=SceneHome&token=&user_id=&version=4.5" andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
            
            self.fllHomeModel = [FLLHomeModel modelWithDic:result];
//        NSLog(@"%@", self.fllHomeModel.data.widgetList[0].widget_data[0].content);
            
            [self.recommendTableView reloadData];
            self.block();
            [self handleChoseItemCollectionViewModel];
            [self.choseItemCollectionView reloadData];
            [self.horizontalCollectionView reloadData];
    
        } andFailureBlock:^(NSError *error) {
    
        }];

}
- (void)handleChoseItemCollectionViewModel
{
    NSArray<Widget_Data *> *array = self.fllHomeModel.data.widgetList[0].widget_data;
    self.iconItemArray = [NSMutableArray array];
    for (NSDictionary *choseDic in array) {
        if ([[choseDic valueForKey:@"type"] isEqualToString:@"image"]) {
            NSMutableDictionary *choseItemDic = [NSMutableDictionary dictionary];
            [choseItemDic setValue:[choseDic valueForKey:@"content"] forKey:@"image"];
            [self.iconItemArray addObject:choseItemDic];
        }
    }
    self.itemNameArray = [NSMutableArray array];
//    NSArray<Widget_Data *> *array = self.fllHomeModel.data.widgetList[0].widget_data;
//    NSMutableArray *itemNameArray = [NSMutableArray array];
    for (NSDictionary *choseDic in array) {
        if ([[choseDic valueForKey:@"type"] isEqualToString:@"text"]) {
            NSMutableDictionary *choseItemDic = [NSMutableDictionary dictionary];
            [choseItemDic setValue:[choseDic valueForKey:@"content"] forKey:@"text"];
            [self.itemNameArray addObject:choseItemDic];
        }
    }


}

- (void)createRecommendTableView
{
    self.recommendTableView = [[UITableView alloc] init];
    _recommendTableView.delegate = self;
    _recommendTableView.dataSource = self;
    [self.view addSubview:_recommendTableView];
    [_recommendTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-49);
    }];
    
    // 注册
    [self.recommendTableView registerNib:[UINib nibWithNibName:@"FLLRecommendTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"recommendTableCell"];
    [self.recommendTableView registerNib:[UINib nibWithNibName:@"FLLFiveItemsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"fiveItemsTableCell"];
    [self.recommendTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.recommendTableView registerNib:[UINib nibWithNibName:@"FLLExpertTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"expertTableCell"];
    [self.recommendTableView registerNib:[UINib nibWithNibName:@"FLLSelectionTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"selectionTableCell"];
    [self.recommendTableView registerNib:[UINib nibWithNibName:@"FLLFoodTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"foodTableCell"];
    
    // 注册区头视图:
    [self.recommendTableView registerClass:[FLLSectionHeaderView class] forHeaderFooterViewReuseIdentifier:@"headerViewCell"];
    [self.recommendTableView registerClass:[FLLForthSectionHeaderView class] forHeaderFooterViewReuseIdentifier:@"forthSectionHeaderView"];
    [self.recommendTableView registerClass:[FLLFifthSectionHeaderView class] forHeaderFooterViewReuseIdentifier:@"fifthSectionHeaderView"];
    // 注册区尾视图
    [self.recommendTableView registerClass:[FLLSectionFooterView class] forHeaderFooterViewReuseIdentifier:@"footerViewCell"];
    
    
    // 设置分割线样式
    self.recommendTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // 下拉刷新
    self.recommendTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self createPOSTRequest];
        [self.recommendTableView.mj_header endRefreshing];
    }];
}
- (void)createHeaderView
{

    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 2.0)];
    
    // 轮播图

    _cycleView = [[UIView alloc] init];
    [_headerView addSubview:_cycleView];

    [_cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(_headerView.mas_height).multipliedBy(0.3);
    }];
    
    self.cycleImage = [[FLLCycleImage alloc] init];
    [_cycleView addSubview:_cycleImage];
    [_cycleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    
    // 轮播图赋值
    __weak FLLRecommendViewController *weakSelf = self;
    self.block = ^(){
        weakSelf.cycleImage.mainImageArray = (NSMutableArray *)weakSelf.fllHomeModel.data.banner;
        
    };

    // 搜索栏
    self.searchBar = [[UISearchBar alloc] init];
    self.searchBar.placeholder = @"输入菜名或食材搜索";
    self.searchBar.delegate = self;
    [_headerView addSubview:self.searchBar];
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(_cycleView.mas_bottom);
        make.height.mas_equalTo(_headerView.mas_height).multipliedBy(0.15);
    }];
      
    
    // 新手入门等4个Item的collection
    _choseItemView = [[UIView alloc] init];
    [_headerView addSubview:_choseItemView];
    [_choseItemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.searchBar.mas_bottom);
        make.height.mas_equalTo(_headerView.mas_height).multipliedBy(0.3);
    }];
    [self createChoseItemCollectionView];
    
    
    // 灰色一条线
    UILabel *lineLabel = [[UILabel alloc] init];
    [_headerView addSubview:lineLabel];
    lineLabel.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.00];
    [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.width.mas_equalTo(_headerView.mas_width).mas_offset(10);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(_choseItemView.mas_bottom);
    }];
    
    // 横拨CollectionView
    self.horizontalView = [[UIView alloc] init];
    [_headerView addSubview:_horizontalView];
    _horizontalView.backgroundColor = [UIColor cyanColor];
    [_horizontalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(lineLabel.mas_bottom);
    }];
    [self createHorizontalCollectionView];
    
    self.recommendTableView.tableHeaderView = _headerView;
    

}
// 创建新手入门等collectionView
- (void)createChoseItemCollectionView
{
    
    UICollectionViewFlowLayout *choseItemLayout = [[UICollectionViewFlowLayout alloc] init];
    choseItemLayout.minimumInteritemSpacing = 0;
    choseItemLayout.minimumLineSpacing = 0;
    choseItemLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.choseItemCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:choseItemLayout];
    _choseItemCollectionView.delegate = self;
    _choseItemCollectionView.dataSource = self;
    _choseItemCollectionView.backgroundColor = [UIColor whiteColor];
    [_choseItemView addSubview:_choseItemCollectionView];
    [_choseItemCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    [_choseItemCollectionView registerNib:[UINib nibWithNibName:@"FLLChoseItemCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"choseItemCollectionCell"];
}
// 创建横拨CollectionView
- (void)createHorizontalCollectionView
{
    UICollectionViewFlowLayout *horizontalLayout = [[UICollectionViewFlowLayout alloc] init];
    horizontalLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    horizontalLayout.minimumInteritemSpacing = 0;
    horizontalLayout.minimumLineSpacing = 0;
    
    self.horizontalCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:horizontalLayout];
    _horizontalCollectionView.backgroundColor = [UIColor whiteColor];
    _horizontalCollectionView.delegate = self;
    _horizontalCollectionView.dataSource = self;
    _horizontalCollectionView.showsHorizontalScrollIndicator = NO;
    [_horizontalView addSubview:_horizontalCollectionView];
    
    [_horizontalCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
        
    }];
    [_horizontalCollectionView registerNib:[UINib nibWithNibName:@"FLLHorizontalCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"horizontalCollectionCell"];
    
    
}
#pragma mark -- TableView DataSouce
// cell 个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 3) {
        
        return 0;
    } else if (section == 4) {
        return 3;
    } else if (section == 6) {
        return 2;
    } else {
        
        return 1;
    }

}
// section个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    if (self.fllHomeModel.data.widgetList != 0) {
        return self.fllHomeModel.data.widgetList.count - 2;
    }
    
    return 0;
}
// cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        FLLRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recommendTableCell"];

        // 传值
        // 数据解析
        // <1> 图片
        
        NSArray<Widget_Data *> *array = self.fllHomeModel.data.widgetList[indexPath.section + 2].widget_data;
        NSMutableArray *todayImageArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"image"]) {
                NSMutableDictionary *imageDic = [NSMutableDictionary dictionary];
                [imageDic setValue: [dic valueForKey:@"content"] forKey:@"image"];
                [todayImageArray addObject:imageDic];
            }

        cell.todayImageArray = todayImageArray;
        };
        
        // <2> 传title
        NSMutableArray *todayTitleArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"text"] && [[dic valueForKey:@"id_Data"] integerValue] % 2 == 1) {
                NSMutableDictionary *titleDic = [NSMutableDictionary dictionary];
                [titleDic setValue:[dic valueForKey:@"content"]forKey:@"tilteText"];
                [todayTitleArray addObject:titleDic];
            }
        }

        cell.todayTitltArray = todayTitleArray;
      
        // <3> 传contentText
        NSMutableArray *todayContentArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"text"] && [[dic valueForKey:@"id_Data"] integerValue] % 2 == 0) {
                NSMutableDictionary *titleDic = [NSMutableDictionary dictionary];
                [titleDic setValue:[dic valueForKey:@"content"]forKey:@"contentText"];
                [todayContentArray addObject:titleDic];
            }
        }
        cell.contentArray = todayContentArray;
        
        // <4> video
        NSMutableArray *todayVideoArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"video"]) {
                NSMutableDictionary *videoDic = [NSMutableDictionary dictionary];
                [videoDic setValue:[dic valueForKey:@"content"]forKey:@"video"];
                [todayVideoArray addObject:videoDic];
            }
        }
        cell.todayVideoArray = todayVideoArray;
        
        // <5> 整理idString
        
        self.todayIDArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"image"]) {
                NSMutableDictionary *idDic = [NSMutableDictionary dictionary];
                [idDic setValue: [dic valueForKey:@"link"] forKey:@"link"];
                [self.todayIDArray addObject:idDic];
            }

        }
        cell.todayIDArray = _todayIDArray;
        
        // 跳转页面
        cell.idBlock = ^(NSString *idString){
        
            FLLMakeFoodViewController *foodVC = [[FLLMakeFoodViewController alloc] init];
            foodVC.idString = idString;
            [self.navigationController pushViewController:foodVC animated:YES];

        };
        
        // 跳转到视频
        cell.videoBlock = ^(NSString *videoString){
        
//            FLLVideoViewController *videoVC = [[FLLVideoViewController alloc] init];
//            NSString *finalString = [videoString componentsSeparatedByString:@"#"].lastObject;
//            videoVC.linkString = finalString;
//            [self.navigationController pushViewController:videoVC animated:YES];
        };

        
        return cell;
    } else if (indexPath.section >= 1 && indexPath.section <= 2) {
        
        FLLFiveItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fiveItemsTableCell"];
        // 传值
        NSArray<Widget_Data *> *fiveItemArray = self.fllHomeModel.data.widgetList[indexPath.section + 2].widget_data;
        cell.idString = self.fllHomeModel.data.widgetList[indexPath.section + 2].title_link;
        cell.fiveItemArray = fiveItemArray;
        
        // 弹出详情页面
        cell.sceneBlock =  ^(NSMutableString *idString){
        
            FLLSceneInfoViewController *sceneInfoVC  = [[FLLSceneInfoViewController alloc] init];
            [self.navigationController pushViewController:sceneInfoVC animated:YES];
            sceneInfoVC.idString = idString;
           
           
        };
        // 弹出MakeFood界面
        cell.foodBlock = ^(NSString *idString){
            FLLMakeFoodViewController *foodVC = [[FLLMakeFoodViewController alloc] init];
            [self.navigationController pushViewController:foodVC animated:YES];
            foodVC.idString = idString;
        };
        return cell;
        
        
        // 推荐达人
    }  else if (indexPath.section == 4) {
        FLLExpertTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"expertTableCell"];
        // 传值
        NSArray<Widget_Data *> *experArray = self.fllHomeModel.data.widgetList[indexPath.section + 2].widget_data;
        NSMutableArray *userPicArray = [NSMutableArray array];
        for (NSDictionary *dic in experArray) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"image"]) {
                NSMutableDictionary *imageDic = [NSMutableDictionary dictionary];
                [imageDic setValue:[dic valueForKey:@"content"]forKey:@"image"];
                [userPicArray addObject:imageDic];
            }
        }
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:userPicArray[indexPath.row][@"image"]] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        }];
        
        NSMutableArray *userNameArray = [NSMutableArray array];
        for (NSDictionary *dic in experArray) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"text"] && [[dic valueForKey:@"id_Data"] integerValue] % 4 == 2) {
                NSMutableDictionary *imageDic = [NSMutableDictionary dictionary];
                [imageDic setValue:[dic valueForKey:@"content"]forKey:@"name"];
                [userNameArray addObject:imageDic];
            }
        }
        cell.userNameLabel.text = userNameArray[indexPath.row][@"name"];
        cell.userNameLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];
        
        NSMutableArray *contentArray = [NSMutableArray array];
        for (NSDictionary *dic in experArray) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"text"] && [[dic valueForKey:@"id_Data"] integerValue] % 4 == 3) {
                NSMutableDictionary *imageDic = [NSMutableDictionary dictionary];
                [imageDic setValue:[dic valueForKey:@"content"]forKey:@"content"];
                [contentArray addObject:imageDic];
            }
        }
        cell.contentLabel.text = contentArray[indexPath.row][@"content"];
        cell.contentLabel.font = [UIFont systemFontOfSize:13 * kWidthScale];
        NSMutableArray *fansArray = [NSMutableArray array];
        for (NSDictionary *dic in experArray) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"text"] && [[dic valueForKey:@"id_Data"] integerValue] % 4 == 0) {
                NSMutableDictionary *imageDic = [NSMutableDictionary dictionary];
                [imageDic setValue:[dic valueForKey:@"content"]forKey:@"count"];
                [fansArray addObject:imageDic];
            }
        }
        cell.fansCount.text = fansArray[indexPath.row][@"count"];
        cell.fansCount.font = [UIFont systemFontOfSize:13 * kWidthScale];

   
        return cell;
        
        // 精选作品
    } else if (indexPath.section == 5 ) {
        FLLSelectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectionTableCell"];
        
        // 传值
        NSArray<Widget_Data *> *selectionArray = self.fllHomeModel.data.widgetList[indexPath.section + 2].widget_data;
        cell.selectionArray = selectionArray;
        

        return cell;
        
        // 美食专题
    } else if (indexPath.section == 6) {
        FLLFoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodTableCell"];
       
        // 上数据
        self.foodArray = self.fllHomeModel.data.widgetList[indexPath.section + 2].widget_data;
        NSMutableArray *PicArray = [NSMutableArray array];
        for (NSDictionary *dic in _foodArray) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"image"]) {
                NSMutableDictionary *imageDic = [NSMutableDictionary dictionary];
                [imageDic setValue:[dic valueForKey:@"content"]forKey:@"image"];
                [PicArray addObject:imageDic];
            }
        }
        // 图片
        [cell.topImageView sd_setImageWithURL:[NSURL URLWithString:PicArray[indexPath.row][@"image"]] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        
        NSMutableArray *userNameArray = [NSMutableArray array];
        for (NSDictionary *dic in _foodArray) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"text"] && [[dic valueForKey:@"id_Data"] integerValue] % 3 == 2) {
                NSMutableDictionary *nameDic = [NSMutableDictionary dictionary];
                [nameDic setValue:[dic valueForKey:@"content"]forKey:@"title"];
                [userNameArray addObject:nameDic];
            }
        }
        // 标题
        cell.titleLabel.text = userNameArray[indexPath.row][@"title"];
        cell.titleLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];

        NSMutableArray *contentArray = [NSMutableArray array];
        for (NSDictionary *dic in _foodArray) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"text"] && [[dic valueForKey:@"id_Data"] integerValue] % 3 == 0) {
                NSMutableDictionary *contentDic = [NSMutableDictionary dictionary];
                [contentDic setValue:[dic valueForKey:@"content"]forKey:@"content"];
                [contentArray addObject:contentDic];
            }
        }
        // 具体内容
        cell.contentLabel.text = contentArray[indexPath.row][@"content"];
        cell.contentLabel.font = [UIFont systemFontOfSize:13 * kWidthScale];


        NSMutableArray *fansArray = [NSMutableArray array];
        for (NSDictionary *dic in _foodArray) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"text"] && [[dic valueForKey:@"id_Data"] integerValue] % 4 == 0) {
                NSMutableDictionary *imageDic = [NSMutableDictionary dictionary];
                [imageDic setValue:[dic valueForKey:@"content"]forKey:@"count"];
                [fansArray addObject:imageDic];
            }
        }
//        cell.fansCount.text = fansArray[indexPath.row][@"count"];
        
        
        return cell;
    } else {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        return cell;
        
    }
}
// cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        return SCREEN_HEIGHT / 3.0 ;
    } else if (indexPath.section == 4) {
        
        return 100 * kHeightScale;
    } else if (indexPath.section == 5){
        return 240 * kHeightScale;
    } else {
        return 200 * kHeightScale;
    }
}
// 区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (section == 3) {
        FLLForthSectionHeaderView *forthSectionHeaderView = [[FLLForthSectionHeaderView alloc] initWithReuseIdentifier:@"forthSectionHeaderView"];
        // 赋值
        forthSectionHeaderView.titleLabel.text = self.fllHomeModel.data.widgetList[section + 2].title;
        forthSectionHeaderView.titleLabel.font = [UIFont systemFontOfSize:17 * kWidthScale];
        [forthSectionHeaderView.moreButton addTarget:self action:@selector(didClickSceneMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        
        return forthSectionHeaderView;
    }  else if (section == 4) {
        FLLFifthSectionHeaderView *fifthSectionHeaderView = [[FLLFifthSectionHeaderView alloc] initWithReuseIdentifier:@"fifthSectionHeaderView"];
        // 赋值
        fifthSectionHeaderView.titleLabel.text = self.fllHomeModel.data.widgetList[section + 2].title;
        fifthSectionHeaderView.titleLabel.font = [UIFont systemFontOfSize:17 * kWidthScale];
        [fifthSectionHeaderView.moreButton addTarget:self action:@selector(didClickRecommendTalentButton:) forControlEvents:UIControlEventTouchUpInside];
        
        return fifthSectionHeaderView;
    } else {
        FLLSectionHeaderView *sectionHeaderView = [[FLLSectionHeaderView alloc] initWithReuseIdentifier:@"headerViewCell"];
        // 赋值
        sectionHeaderView.titleLabel.text = self.fllHomeModel.data.widgetList[section + 2].title;
        sectionHeaderView.titleLabel.font = [UIFont systemFontOfSize:17 * kWidthScale];
        if (section == 0) {
            [sectionHeaderView.moreButton addTarget:self action:@selector(didClickTodayMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        if (section == 1) {
            [sectionHeaderView.moreButton addTarget:self action:@selector(didClickMealMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        if (section == 2) {
            [sectionHeaderView.moreButton addTarget:self action:@selector(didClickJuiceMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        if (section == 5) {
            [sectionHeaderView.moreButton addTarget:self action:@selector(didClickSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        if (section == 6) {
            [sectionHeaderView.moreButton addTarget:self action:@selector(didClickFoodMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        
        return sectionHeaderView;
    }
}
/****************        button点击事件      ******************/
- (void)didClickTodayMoreButton:(UIButton *)button
{

    FLLSceneInfoViewController *sceneInfoVC = [[FLLSceneInfoViewController alloc] init];
    NSMutableString *idString = (NSMutableString *)self.fllHomeModel.data.widgetList[2].title_link;
    sceneInfoVC.idString = idString;
     [self.navigationController pushViewController:sceneInfoVC animated:YES];
    
}
- (void)didClickMealMoreButton:(UIButton *)button
{
    
    FLLSceneInfoViewController *sceneInfoVC = [[FLLSceneInfoViewController alloc] init];
    NSMutableString *idString = (NSMutableString *)self.fllHomeModel.data.widgetList[3].title_link;
    sceneInfoVC.idString = idString;
    [self.navigationController pushViewController:sceneInfoVC animated:YES];
    
}
- (void)didClickJuiceMoreButton:(UIButton *)button
{
    
    FLLSceneInfoViewController *sceneInfoVC = [[FLLSceneInfoViewController alloc] init];
    NSMutableString *idString = (NSMutableString *)self.fllHomeModel.data.widgetList[4].title_link;
    sceneInfoVC.idString = idString;
    [self.navigationController pushViewController:sceneInfoVC animated:YES];
    
}
- (void)didClickSceneMoreButton:(UIButton *)button
{

    FLLSceneViewController *allSceneVC = [[FLLSceneViewController alloc] init];
    NSString *titleString = self.fllHomeModel.data.widgetList[5].title;
    NSString *finalString = [titleString substringToIndex:4];
    allSceneVC.navigationItem.title = finalString;
        [self.navigationController pushViewController:allSceneVC animated:YES];
}
- (void)didClickRecommendTalentButton:(UIButton *)button
{
    FLLRecommendTalentViewController *recommendTalentVC = [[FLLRecommendTalentViewController alloc] init];
    NSString *titleString = self.fllHomeModel.data.widgetList[6].title;
    recommendTalentVC.navigationItem.title = titleString;
    [self.navigationController pushViewController:recommendTalentVC animated:YES];
    
}
- (void)didClickSelectedButton:(UIButton *)button
{

    FLLSelectedMoreViewController *selectedMoreVC = [[FLLSelectedMoreViewController alloc] init];
    selectedMoreVC.navigationItem.title = self.fllHomeModel.data.widgetList[7].title;
    [self.navigationController pushViewController:selectedMoreVC animated:YES];
    
}
- (void)didClickFoodMoreButton:(UIButton *)button
{

    FLLFoodMoreViewController *foodMoreVC = [[FLLFoodMoreViewController alloc] init];
   
    foodMoreVC.navigationItem.title = self.fllHomeModel.data.widgetList[8].title;
     [self.navigationController pushViewController:foodMoreVC animated:YES];

}

// 区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 50;
}
// 改变区头视图颜色
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    
    if (section == 3) {
        
        view.tintColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.00];
    }
}
// 取消分区头视图 和 尾视图跟随
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 还没研究原理
    CGFloat sectionHeaderHeight = 50 ;//(头视图的高度)
    CGFloat sectionFooterHeight = 40;// (尾视图的高度)
    CGFloat f = scrollView.contentSize.height - scrollView.frame.size.height - scrollView.contentOffset.y-sectionFooterHeight;
    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(- scrollView.contentOffset.y, 0, -sectionFooterHeight, 0);
    }else if ((scrollView.contentOffset.y >= sectionHeaderHeight) && (f >= 0)) {
        scrollView.contentInset = UIEdgeInsetsMake(- sectionHeaderHeight, 0, - sectionFooterHeight, 0);
    }else if ((f >= - sectionFooterHeight) && (f <= 0)){
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, - (sectionFooterHeight + f), 0);
    }else if (f < - sectionFooterHeight){
        scrollView.contentInset = UIEdgeInsetsMake(- sectionHeaderHeight, 0, 0, 0);
    }
    
    
}
// 区尾视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    FLLSectionFooterView *footerView = [[FLLSectionFooterView alloc] initWithReuseIdentifier:@"footerViewCell"];
    [footerView.titleLabel setPropetyWithTextColor:[UIColor lightGrayColor] Alignment:1 textFont:[UIFont systemFontOfSize:14 * kWidthScale]];
    footerView.titleLabel.text = self.fllHomeModel.data.widgetList[section + 2].desc;

    return footerView;
}
// 区尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0 || (section <= 6 && section >= 3)) {
        return 0;
    }

    return 40;
}
// 改变区尾视图的颜色
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{

    view.tintColor = [UIColor whiteColor];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

   
    if (indexPath.section == 6) {
        FLLFoodWebVC *webVC = [[FLLFoodWebVC alloc] init];
        [self.navigationController pushViewController:webVC animated:YES];
        // 整理出link后传出
        NSMutableArray *linkArray = [NSMutableArray array];
        for (NSDictionary *dic in _foodArray) {
            if ([[dic valueForKey:@"type"] isEqualToString:@"image"]) {
                NSMutableDictionary *linkDic = [NSMutableDictionary dictionary];
                [linkDic setValue:[dic valueForKey:@"link"]forKey:@"link"];
                [linkArray addObject:linkDic];
            }
        }
        NSLog(@"%@*********", linkArray);
        
        webVC.linkString = linkArray[indexPath.row][@"link"];
      
        
    }
}
#pragma mark -- collectionView dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _choseItemCollectionView) {
        return _iconItemArray.count;
     }
    return self.fllHomeModel.data.widgetList[1].widget_data.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _choseItemCollectionView) {
        FLLChoseItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"choseItemCollectionCell" forIndexPath:indexPath];
        // 赋值
        [cell.iconItem sd_setImageWithURL:[NSURL URLWithString:self.iconItemArray[indexPath.row][@"image"]] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        }];
        cell.itemName.text = _itemNameArray[indexPath.row][@"text"];
        cell.itemName.font = [UIFont systemFontOfSize:14 * kWidthScale];
        cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
           return cell;
    }
    FLLHorizontalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"horizontalCollectionCell" forIndexPath:indexPath];
    // 赋值
    NSArray<Widget_Data *> *horizontalArray = self.fllHomeModel.data.widgetList[1].widget_data;
    [cell.horizontalImage sd_setImageWithURL:[NSURL URLWithString:horizontalArray[indexPath.row].content] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    }];
    return cell;
    
    
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (collectionView == _choseItemCollectionView) {
        return CGSizeMake(_choseItemView.bounds.size.width / 4.0, _choseItemView.bounds.size.height);
    }
    return CGSizeMake(SCREEN_WIDTH / 2.0, _horizontalCollectionView.bounds.size.height);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if(collectionView == _choseItemCollectionView) {
        if (indexPath.row == 0) {
            FLLGreenHandViewController *greenHandVC = [[FLLGreenHandViewController alloc] init];
            [self.navigationController pushViewController:greenHandVC animated:YES];
            greenHandVC.navigationItem.title = _itemNameArray[indexPath.row][@"text"];
        } if (indexPath.row == 1) {
        
            FLLMatchViewController *matchVC = [[FLLMatchViewController alloc] init];
            [self.navigationController pushViewController:matchVC animated:YES];
            matchVC.navigationItem.title = _itemNameArray[indexPath.row][@"text"];
        } if (indexPath.row == 2) {
            FLLSceneViewController *sceneVC = [[FLLSceneViewController alloc] init];
            [self.navigationController pushViewController:sceneVC animated:YES];
            sceneVC.navigationItem.title = _itemNameArray[indexPath.row][@"text"];
        } if (indexPath.row == 3) {
            FLLLikeViewController *likeVC = [[FLLLikeViewController alloc] init];
            [self.navigationController pushViewController:likeVC animated:YES];
            likeVC.navigationItem.title = _itemNameArray[indexPath.row][@"text"];
        }
    
    }
    if (collectionView == _horizontalCollectionView) {
        FLLDetailViewController *detailVC = [[FLLDetailViewController alloc] init];
        [self.navigationController pushViewController:detailVC animated:YES];
        // 将点击的当前下标传出去
        detailVC.currentIndex = indexPath.row;
        detailVC.passString = self.fllHomeModel.data.widgetList[1].widget_data[indexPath.row].link;
    }
    
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{

    FLLSearchViewController *searchVC = [[FLLSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    [self.searchBar resignFirstResponder];
    
}
- (void)viewDidDisappear:(BOOL)animated
{

    [super viewDidDisappear:animated];
    [self.searchBar resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
